home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / unix / c / syslog < prev    next >
Text File  |  1996-11-09  |  6KB  |  232 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/syslog,v $
  4.  * $Date: 1996/10/30 22:04:51 $
  5.  * $Revision: 1.1 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: syslog,v $
  10.  * Revision 1.1  1996/10/30 22:04:51  unixlib
  11.  * Initial revision
  12.  *
  13.  ***************************************************************************/
  14.  
  15. static const char rcs_id[] = "$Id: syslog,v 1.1 1996/10/30 22:04:51 unixlib Rel $";
  16.  
  17. /*
  18.  * Copyright (c) 1983, 1988, 1993
  19.  *    The Regents of the University of California.  All rights reserved.
  20.  *
  21.  * Redistribution and use in source and binary forms, with or without
  22.  * modification, are permitted provided that the following conditions
  23.  * are met:
  24.  * 1. Redistributions of source code must retain the above copyright
  25.  *    notice, this list of conditions and the following disclaimer.
  26.  * 2. Redistributions in binary form must reproduce the above copyright
  27.  *    notice, this list of conditions and the following disclaimer in the
  28.  *    documentation and/or other materials provided with the distribution.
  29.  * 3. All advertising materials mentioning features or use of this software
  30.  *    must display the following acknowledgement:
  31.  *    This product includes software developed by the University of
  32.  *    California, Berkeley and its contributors.
  33.  * 4. Neither the name of the University nor the names of its contributors
  34.  *    may be used to endorse or promote products derived from this software
  35.  *    without specific prior written permission.
  36.  *
  37.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  38.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  41.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  43.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47.  * SUCH DAMAGE.
  48.  */
  49.  
  50. #if defined(LIBC_SCCS) && !defined(lint)
  51. static char sccsid[] = "@(#)syslog.c    8.4 (Berkeley) 3/18/94";
  52. #endif /* LIBC_SCCS and not lint */
  53.  
  54. #include <sys/types.h>
  55. #include <sys/socket.h>
  56. #include <sys/syslog.h>
  57. #include <sys/uio.h>
  58. #include <netdb.h>
  59.  
  60. #include <errno.h>
  61. #include <fcntl.h>
  62. #include <paths.h>
  63. #include <stdio.h>
  64. #include <string.h>
  65. #include <time.h>
  66. #include <unistd.h>
  67.  
  68. #include <stdarg.h>
  69.  
  70. static int    LogFile = -1;        /* fd for log */
  71. static int    connected;        /* have done connect */
  72. static int    LogStat = 0;        /* status bits, set by openlog() */
  73. static const char *LogTag = NULL;    /* string to tag the entry with */
  74. static int    LogFacility = LOG_USER;    /* default facility code */
  75. static int    LogMask = 0xff;        /* mask of priorities to be logged */
  76. extern char    *__progname;        /* Program name, from crt0. */
  77.  
  78. /*
  79.  * syslog, vsyslog --
  80.  *    print message on log file; output is intended for syslogd(8).
  81.  */
  82. void
  83. syslog(int pri, const char *fmt, ...)
  84. {
  85.     va_list ap;
  86.  
  87.     va_start(ap, fmt);
  88.     vsyslog(pri, fmt, ap);
  89.     va_end(ap);
  90. }
  91.  
  92. void
  93. vsyslog(int pri, const char *fmt, va_list ap)
  94. {
  95.     register int cnt;
  96.     register char ch, *p, *t;
  97.     time_t now;
  98.     int fd, saved_errno;
  99.     char *stdp, tbuf[2048], fmt_cpy[1024];
  100.  
  101. #define    INTERNALLOG    LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
  102.     /* Check for invalid bits. */
  103.     if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
  104.         syslog(INTERNALLOG,
  105.             "syslog: unknown facility/priority: %x", pri);
  106.         pri &= LOG_PRIMASK|LOG_FACMASK;
  107.     }
  108.  
  109.     /* Check priority against setlogmask values. */
  110.     if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
  111.         return;
  112.  
  113.     saved_errno = errno;
  114.  
  115.     /* Set default facility if none specified. */
  116.     if ((pri & LOG_FACMASK) == 0)
  117.         pri |= LogFacility;
  118.  
  119.     /* Build the message. */
  120.     (void)time(&now);
  121.     p = tbuf + sprintf(tbuf, "<%d>", pri);
  122.     p += strftime(p, sizeof (tbuf) - (p - tbuf), "%h %e %T ",
  123.         localtime(&now));
  124.     if (LogStat & LOG_PERROR)
  125.         stdp = p;
  126.     if (LogTag == NULL)
  127.         LogTag = __progname;
  128.     if (LogTag != NULL)
  129.         p += sprintf(p, "%s", LogTag);
  130.     if (LogStat & LOG_PID)
  131.         p += sprintf(p, "[%d]", getpid());
  132.     if (LogTag != NULL) {
  133.         *p++ = ':';
  134.         *p++ = ' ';
  135.     }
  136.  
  137.     /* Substitute error message for %m. */
  138.     for (t = fmt_cpy; ch = *fmt; ++fmt)
  139.         if (ch == '%' && fmt[1] == 'm') {
  140.             ++fmt;
  141.             t += sprintf(t, "%s", strerror(saved_errno));
  142.         } else
  143.             *t++ = ch;
  144.     *t = '\0';
  145.  
  146.     p += vsprintf(p, fmt_cpy, ap);
  147.     cnt = p - tbuf;
  148.  
  149.     /* Output to stderr if requested. */
  150.     if (LogStat & LOG_PERROR) {
  151.         struct iovec iov[2];
  152.         register struct iovec *v = iov;
  153.  
  154.         v->iov_base = stdp;
  155.         v->iov_len = cnt - (stdp - tbuf);
  156.         ++v;
  157.         v->iov_base = "\n";
  158.         v->iov_len = 1;
  159.         (void)writev(STDERR_FILENO, iov, 2);
  160.     }
  161.  
  162.     /* Get connected, output the message to the local logger. */
  163.     if (!connected)
  164.         openlog(LogTag, LogStat | LOG_NDELAY, 0);
  165.     if (send(LogFile, tbuf, cnt, 0) >= 0)
  166.         return;
  167.  
  168.     /*
  169.      * Output the message to the console; don't worry about blocking,
  170.      * if console blocks everything will.  Make sure the error reported
  171.      * is the one from the syslogd failure.
  172.      */
  173.     if (LogStat & LOG_CONS &&
  174.         (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
  175.         (void)strcat(tbuf, "\r\n");
  176.         cnt += 2;
  177.         p = index(tbuf, '>') + 1;
  178.         (void)write(fd, p, cnt - (p - tbuf));
  179.         (void)close(fd);
  180.     }
  181. }
  182.  
  183. static struct sockaddr SyslogAddr;    /* AF_UNIX address of local logger */
  184.  
  185. void
  186. openlog(char *ident, int logstat, int logfac)
  187. {
  188.     if (ident != NULL)
  189.         LogTag = ident;
  190.     LogStat = logstat;
  191.     if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  192.         LogFacility = logfac;
  193.  
  194.     if (LogFile == -1) {
  195.         SyslogAddr.sa_family = AF_UNIX;
  196.         (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
  197.             sizeof(SyslogAddr.sa_data));
  198.         if (LogStat & LOG_NDELAY) {
  199.             if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
  200.                 return;
  201.             (void)fcntl(LogFile, F_SETFD, 1);
  202.         }
  203.     }
  204.     if (LogFile != -1 && !connected)
  205.         if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
  206.             (void)close(LogFile);
  207.             LogFile = -1;
  208.         } else
  209.             connected = 1;
  210. }
  211.  
  212. void
  213. closelog (void)
  214. {
  215.     (void)close(LogFile);
  216.     LogFile = -1;
  217.     connected = 0;
  218. }
  219.  
  220. /* setlogmask -- set the log mask level */
  221. int
  222. setlogmask(pmask)
  223.     int pmask;
  224. {
  225.     int omask;
  226.  
  227.     omask = LogMask;
  228.     if (pmask != 0)
  229.         LogMask = pmask;
  230.     return (omask);
  231. }
  232.